import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== antd
import { DownCircleOutlined  } from '@ant-design/icons'
import { Divider } from 'antd'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const $time 			= $Lazy.file(()=>import('@com/utils/time'))
const ExpandComponent 	= $Lazy.state(()=>import('./cpt/expands'))
const DescriptionTable	= $Lazy.state(()=>import('./cpt/description-table'))
const Result			= $Lazy.state(()=>import('./cpt/result'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys: [],
		isShow: false,
		// 搜索表单列表
		forms : [
			{ label: '标本条码',				name: 'spec_code' },
			{ label: '第三方条码',			name: 'third_spec_code' },
			// { label: '实验号',				name: 'experiment_num' },
			{ label: '医院',				name: 'hosp_id',							type: 'select' },
			{ label: '接收时间', 			names: ['ck_start_date','ck_end_date'],		type:'date-range', 	name:'jz-time' },
			{ label: '病人姓名',				name: 'patient_name' },
			{ label: '项目名称',				name: 'kind_name',							type: 'select' },
			// { label: ' ',					tag:'仅显示自然项目', name: 'just_parent_kind_name' , type: 'checkbox', colon:false},
			// { label: '检测时间',  			names: ['ts_start_date','ts_end_date'], 	type:'date-range', 	name:'jz-time_1' },
			// { label: '报告时间',  			names: ['rp_start_date','rp_end_date'], 	type:'date-range',	name:'jz-time_2'},
			{ label: '科室',				name: 'parent_project_id',					type: 'select',		idStr:'id' },
			{ label: '岗位',				name: 'project_id',							type: 'select' },
			{ label: '证件号',				name: 'card_no' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['7']()
			this.resetModel = { ck_start_date: arr[0], ck_end_date: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			if(window.isInit){
				this.fetch()
			}
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state

		$cache.then( async f => {
			// 科室
			$fn.setRow( forms, 'parent_project_id', await f.getCache({ api:'ke_shi' }) )
			// 岗位
			$fn.setRow( forms, 'project_id', await f.getCache({ api:'gang_wei' }) )
			// 医院
			$fn.setRow( forms, 'hosp_id', await f.getCache({ api:'hospital' }) )
			// 项目名称
			$fn.setRow( forms, 'kind_name', await f.getCache({ api: 'project_select' }))
			//
			this.setState({ forms })
		})
	}
	 
	// paging
	fetch = () => $fn.fetch.call(this,'specimen/views', {isIndex:true})
	
	// 表单
	cols = [
		{ title: '标本条码',			dataIndex: 'spec_code', 			...$fn.table.spec_code },
		{ title: '医院名称', 		dataIndex: 'hosp_name', 			width:200 },
		{ title: '自然项目', 		dataIndex: 'parent_kind_name', 		width:220 },
		{ title: '病人姓名', 		dataIndex: 'patient_name', 			...$fn.table.name },
		{ title: '受检人类型', 		dataIndex: 'case_type_name', 		width:150 },
		{ title: '年龄',			dataIndex: 'age_first_type_name', 	...$fn.table.age },
		{ title: '性别', 			dataIndex: 'sex_name', 				...$fn.table.sex },
		// { title: '项目名称', 		dataIndex: 'kind_name', 			width: 250 },
		{ title: '标本类型', 		dataIndex: 'spec_type_name', 		width:200 },
		// { title: '结果', 			dataIndex: 'result', 				width:150 },
		// { title: '提示',			dataIndex: 'tips', 					width:100 },
		{ title: '第三方条码',		dataIndex: 'third_spec_code', 		...$fn.table.spec_num },
		{ title: '接收时间', 		dataIndex: 'receive_time', 			...$fn.table.time },
		{ title: '项目类型', 		dataIndex: 'kind_category_name', 	width:100 },
		{ title: '项目代码', 		dataIndex: 'kind_code', 			width:150 },
		// { title: '实验号', 			dataIndex: 'experiment_num', 		width:150 },
		{ title: '岗位', 			dataIndex: 'project_name', 			width:150 },
		// { title: '报告时间',			dataIndex: 'report_time', 			...$fn.table.time },
		// { title: '报告状态', 		dataIndex: 'rp_status_name', 		width:150 },
		{ title: '状态', 			dataIndex: 'status_name', 			width:150,render:(text,{status,status_name})=>{return status==="45006"?"已迟发":status_name}},
		/* 待接收状态显示标本箱 2021-7-26 --马婷 */
		{ title: '标本箱', 			dataIndex: 'box_number', 			width:100 },
		{ title: '送检医生', 		dataIndex: 'doctor', 				...$fn.table.name },
		// { title: '检测时间', 		dataIndex: 'check_time', 			...$fn.table.time },
		{ title: '证件类型', 		dataIndex: 'card_type_name', 		...$fn.table.card_type },
		{ title: '证件号', 			dataIndex: 'card_no', 				...$fn.table.idcard },
		{ title: '科室', 			dataIndex: 'department_name', 		width:150 },
		{ title: '床号', 			dataIndex: 'bed_no', 				...$fn.table.num },
	]
	// 操作按钮
	controls =  () => {
		const { ck_start_date, ck_end_date } = this.model
		return [
			// 文件导出异步,添加isSock 2022-4-11 
			Controls.exports.call(this,  'specimen/viewsexport', { param:{ck_start_date, ck_end_date}, isConfirm:true, isSock: true }),
			Controls.setTableHeader.call(this),
		]
	}
	
	render(){
		const isService = window.location.hash.indexOf('service-inquiry') >= 0
		const { isShow } = this.state
		return (
			<>
				<Page title={isService?'客服查询':'标本查询'} nobc controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'7' }) }
					{/* 表格 */}
					<div className='fx ex bcf'>
						{ Table.call(this, { type:1, isIndex:true, sort:false, setHeader:true, isInit: window.isInit, onRow: rows => {
							if(this.descriptionTableRef?.load && this.expandRef?.setValue){
								this.descriptionTableRef.load(rows)
								this.expandRef.setValue(rows)
								this.resultRef.load(rows)
							}else{
								setTimeout(()=>{
									this.expandRef.setValue(rows)
									this.descriptionTableRef.load(rows)
									this.resultRef.load(rows)
								}, 500)
							}
						} }) }
						<div className='fv' style={{width: '30%'}}>
							<Result onRef={ref=>this.resultRef=ref} />
						</div>
					</div>
					{/* 收缩部分 */}
					<div className='bcf'>
						<Divider className='miniDivider' style={{margin: 0}}>
							<span className='f13 c0 cp' onClick={ ()=> {this.setState({ isShow: !isShow}, ()=>{this.table.resize()})} }>
								{ isShow ? <>收起 <DownCircleOutlined style={{transform: 'rotate(180deg)'}} dashed='true' /></> : <>展开 <DownCircleOutlined dashed='true'  /></> }
							</span>
						</Divider>
					</div>
					<div style={{display:isShow?'block':'none'}}>
						<div className='fx' style={{height:350}}>
							{/* 表单 */}
							<ExpandComponent onRefresh={this.fetch} onRef={ref=>this.expandRef = ref} />
							{/* 描述 */}
							<DescriptionTable onRef={ref=>this.descriptionTableRef = ref} />
						</div>
					</div>
				</Page>
			</>
		)
	}
}

export default Index